package serivce;

import dao.FangZhiDao;
import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import untity.*;

import java.util.List;

@Service
public class UserServiceImp implements  UserSerivce{
    @Autowired
    private UserDao userDao;
    @Autowired
    private FangZhiDao fangZhiDao;
    @Override
    public boolean collection(HouseAndUser houseAndUser) {
        return userDao.collection(houseAndUser);
    }

    @Override
    public boolean cancelCollection(HouseAndUser houseAndUser) {
        return userDao.cancelCollection(houseAndUser);
    }

    @Override
    public Boolean checkCollection(HouseAndUser houseAndUser) {
        Boolean a= userDao.checkCollection(houseAndUser);

        return a;
    }

    @Override
    public List<BriefHouse> searchByUserID(String id,Integer count) {
        List<BriefHouse> list =userDao.searchByUserID(id,count*70);
        String imgpath=null;
        for (BriefHouse b:list){
            if (fangZhiDao.searchImg(b.getId()).length!=0){
                imgpath=fangZhiDao.searchImg(b.getId())[0];
            }else {
                imgpath=null;
            }
            b.setImgPath(imgpath);
        }
        return list;
    }

    @Override
    public List<Myrental> searchMyRental(String id,Integer count) {
        List<Myrental> list=userDao.searchMyRental(id,count*70);
        for (Myrental re:list) {
            System.out.println(re);
        }
        String imgpath=null;
        for (Myrental b:list){
            if (fangZhiDao.searchImg(b.getId()).length!=0){
                imgpath=fangZhiDao.searchImg(b.getId())[0];
            }else {
                imgpath=null;
            }
            b.setImgPath(imgpath);
        }
        return list;
    }

    @Override
    public void creatHistory(String userId) {
        for (int i=0;i<100;i++){
            userDao.createHistory(userId);
        }

    }

    @Override
    public Boolean addHistory(LiShi liShi) {
        String id=userDao.queryHistory(String.valueOf(liShi.getUserId()));
        liShi.setId(Integer.parseInt(id));
        return userDao.addHistory(liShi);
    }

    @Override
    public List<Histroy> searchHistry(String id,Integer count) {
        List<Histroy> list=userDao.searchHistry(id,count*10);
        String imgpath=null;
        for (Histroy b:list){
            if (fangZhiDao.searchImg(b.getId()).length!=0){
                imgpath=fangZhiDao.searchImg(b.getId())[0];
            }else {
                imgpath=null;
            }
            b.setImgPath(imgpath);
        }
        return list;
    }

    @Override
    public Boolean creatConnection(ChatRoomDB room) {
         return userDao.creatConnection(room);

    }

    @Override
    public List<UserConnection> searchConnetion(String id) {
        List<UserConnection> list= userDao.queryConnetion(id);
        List<UserConnection> list2=userDao.queryConnection2(id);
        list.addAll(list2);
        return list;
    }

    @Override
    public ChatRoomDB queryChatRoom(String userId, String landlordId) {
        ChatRoomDB chatRoomDB= userDao.queryChatRoom(userId,landlordId);
        return chatRoomDB;
    }

    @Override
    public List<String> searchRoomId(String userId) {
        List<String> list=userDao.searchRoomId(userId);
        return list;
    }

    @Override
    public Boolean changeData(BriefHouse house) {
        return userDao.changeData(house);
    }

    @Override
    public void delete(String id) {
        userDao.posi(id);
        userDao.detele(id);
    }
}
